package com.duam.serveradmin;

import static com.duam.serveradmin.Constants.*;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	
    @Override
	protected void onResume() 
    {
    	super.onResume();
    	
    	updatePID();
	}
    
    private void updatePID()
    {
    	final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.proccessing), getResources().getString(R.string.querying_pid));

    	ExecTask task = new ExecTask()
    	{
			@Override
			protected void onPostExecute(String result) 
			{
				((TextView) findViewById(R.id.txtPid)).setText(getResources().getString(R.string.javaPID) +" "+ result);
				pd.dismiss();
			}    		
    	};
    	task.execute(CMD_PIDOF);
    }
    
    public void queryPID(View view)
    {
    	updatePID();
    }
    
    public void startServer(View view) 
    {
    	final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.proccessing), getResources().getString(R.string.starting));
    	
    	ExecTask task = new ExecTask()
    	{
			@Override
			protected void onPostExecute(String result) 
			{
				pd.dismiss();
				
				updatePID();
			}    		
    	};
    	task.execute(CMD_START);
    }
    
    public void stopServer(View view)
    {
    	final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.proccessing), getResources().getString(R.string.stopping));
    	
    	ExecTask task = new ExecTask()
    	{
			@Override
			protected void onPostExecute(String result) 
			{
				((TextView) findViewById(R.id.txtPid)).setText(getResources().getString(R.string.javaPID) +" -");
				pd.dismiss();
			}    		
    	};
    	task.execute(CMD_STOP);
    }

}
